package www.ld.com.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.ld.com.common.CacheKeyConstants;
import www.ld.com.domain.category.pojo.CategoryPO;
import www.ld.com.domain.category.query.CategoryQuery;
import www.ld.com.domain.category.vo.CategoryVO;
import www.ld.com.exception.ValidateException;
import www.ld.com.mapper.CategoryMapper;

import javax.annotation.Resource;

@Service
public class CategoryService {
    @Resource
    private CategoryMapper cateGoryMapper;

    @Resource
    private CategoryCacheService categoryCacheService;


    public IPage<CategoryVO> pageCategoryByCategoryQuery(CategoryQuery categoryQuery) {
        return cateGoryMapper.pageCategoryByCategoryQuery(new Page<>(categoryQuery.getCurrent(), categoryQuery.getSize()), categoryQuery);
    }

    public CategoryPO getCategoryPOById(String id) {
        return cateGoryMapper.selectById(id);
    }


    @Transactional
    @CacheEvict(cacheNames =  CacheKeyConstants.CategoryKey.CACHE_NAME,   key = "'"+CacheKeyConstants.CategoryKey.KEY_FIND_ALL+"'")
    public void saveCategoryPO(CategoryPO categoryPO) {
        validateCategoryParentId(categoryPO.getParentId(), categoryPO.getId());
        cateGoryMapper.insert(categoryPO);
    }

    private void validateCategoryParentId(Long parentId, Long id) {
        parentId = parentId == null ? 0 : parentId;
        if (id != null && Long.compare(parentId, id) == 0) {
            throw new ValidateException("上级分类不能当前分类");
        }
        if (parentId != 0 && StrUtil.isBlank(categoryCacheService.getCategoryNameById(String.valueOf(parentId)))) {
            throw new ValidateException("没有找到上级分类信息");
        }
    }

    @Transactional
    @Caching(evict = {
            @CacheEvict(cacheNames = CacheKeyConstants.CategoryKey.CACHE_NAME, key = "'"+CacheKeyConstants.CategoryKey.KEY_FIND_ALL+"'"),
            @CacheEvict(cacheNames = "category", key = "'"+ CacheKeyConstants.CategoryKey.KEY_GET_CATEGORY_NAME_BY_ID +"'+#p0.id"),
    })

    public void updateCategoryPO(CategoryPO categoryPO) {
        validateCategoryParentId(categoryPO.getParentId(), categoryPO.getId());
        cateGoryMapper.updateById(categoryPO);
    }

    @Transactional
    @Caching(evict = {
            @CacheEvict(cacheNames = CacheKeyConstants.CategoryKey.CACHE_NAME, key = "'"+CacheKeyConstants.CategoryKey.KEY_FIND_ALL+"'"),
            @CacheEvict(cacheNames = CacheKeyConstants.CategoryKey.CACHE_NAME,key = "'"+ CacheKeyConstants.CategoryKey.KEY_GET_CATEGORY_NAME_BY_ID +"'+#p0")
    })
    public void deleteCategoryById(String id) {
        //TODO 判读属性分组信息
        //TODO 判读是否有子集
        cateGoryMapper.deleteById(id);
    }
}
